package io.renren.modules.generator.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.ShiroUtils;
import io.renren.modules.sys.entity.SysUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.generator.entity.CourseEntity;
import io.renren.modules.generator.service.CourseService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 *
 *
 * @author liangzai
 * @email liuyongxu@gmail.com
 * @date 2022-05-25 12:28:33
 */
@RestController
@RequestMapping("generator/course")
public class CourseController {
    @Autowired
    private CourseService courseService;


    /**
     * 管理员课程列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = courseService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 学生选课课程列表
     */
    @RequestMapping("/studentCourse")
    public R studentCourse(@RequestParam Map<String, Object> params){
        params.put("userId",ShiroUtils.getUserId());
        Page<Map<String, Object>> page = new Page<>( Long.valueOf(String.valueOf(params.get("page"))),  Long.valueOf(String.valueOf(params.get("limit"))));
        IPage<Map<String, Object>> mapIPage = courseService.studentCourse(page, params);
        PageUtils newPage = new PageUtils(mapIPage);

        return R.ok().put("page", newPage);
    }

    /**
     * 学生课程列表
     */
    @RequestMapping("/studentList")
    public R studentList(@RequestParam Map<String, Object> params){
        params.put("userId",ShiroUtils.getUserId());
        Page<Map<String, Object>> page = new Page<>( Long.valueOf(String.valueOf(params.get("page"))),  Long.valueOf(String.valueOf(params.get("limit"))));
        IPage<Map<String, Object>> mapIPage = courseService.studentList(page, params);
        PageUtils newPage = new PageUtils(mapIPage);

        return R.ok().put("page", newPage);
    }



    /**
     * 教师课程列表
     */
    @RequestMapping("/teacherList")
    public R teacherList(@RequestParam Map<String, Object> params){
        PageUtils page = courseService.teacherList(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{courseId}")
    public R info(@PathVariable("courseId") Integer courseId){
		CourseEntity course = courseService.getById(courseId);

        return R.ok().put("course", course);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CourseEntity course){
        courseService.saveCourse(course);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CourseEntity course){
		courseService.updateCourse(course);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] courseIds){
        courseService.removeByIds(Arrays.asList(courseIds));

        return R.ok();
    }

    @RequestMapping("/getAllTeacher")
    public R getAllTeacher(){
        List<SysUserEntity> sysUserEntities = courseService.getAllTeacher();
        return R.ok().put("user", sysUserEntities);
    }

    /**
     * 学生选课
     * @param id
     * @return
     */

    @RequestMapping("/selectCourse/{id}")
    public synchronized R selectCourse(@PathVariable("id") Integer id){
        courseService.selectCourse(id);
        return R.ok();
    }

    /**
     * 学生退课
     * @param id
     * @return
     */
    @RequestMapping("/exitCourse/{id}")
    public synchronized R exitCourse(@PathVariable("id") Integer id){
        courseService.exitCourse(id);
        return R.ok();
    }
}
